A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Records Are Under Used


There is nothing wrong with using records. Some prefer using always classes for consistency. But in today's still bandwidth expensive internet era, why haul in the entire classes for a small organized CGI program, as an example? If you do not need to inherit, or if the inheritance will cause more confusion than it will solve.. then consider records.

Also consider StrWrap1 if you feel that A TStringList class is overkill for string organization.


program Project1;

{$mode objfpc}{$H+}

type

 RTest = record
   joe: procedure(s: string);
   yourproc: procedure;
 end;

procedure testing(s: string);
begin
  writeln(s);
end;

procedure yada;
begin
  writeln('yada yada');
end;

var
  Test:RTest;

begin
 test.joe:= @testing;
 test.joe('test');
 test.yourproc:= @yada;
 //how to fire off a procedure without a paramater?
 test.yourproc();   //note brackets
 readln;
end.

Subject: records in procedures, record in a procedure, records in a procedure, procedure record, alternative to class, alternative to classes.
Warning:
Please remember that you should not embed data into your programs when a database is fit for your situation. Many programming books show extremely poor examples of records and structures in use. An example of a poor use of a record is when one stores people's names, cities, and addresses right into the source code of your program. This should be in a database, not in a record. It clutters up your source code and becomes a maintenance nightmare if you embed and store data right in your source code.

You may think that the above warning is obvious, but please be advised that there are several programming books out there which show records being used to store embedded data in your program. Many books show examples of people, phone numbers, and addresses being stored right in the source code. That data is more fit for a database . If you have one of these books that is flawed for that reason, you should politely write a review or commentary on the book, so that other people do not fall into this trap and obtain bad habits. Some book authors just like to fill their books up with any old programming examples which have absolutely no bearing in the real world.

Some Pascal and C books, for example, show using records to store people in a classroom, or people in a school, or people in a city with blue or pink pants, etc. This is incorrect use of a record. Records should be used to organize your CODE, not to embed, organize, and manage this sort of data right within your application!

Even using text files and keeping your data separate from source code that way is better than embedding it right into a source file. At some point, you are going to have to update and change your data. Recompiling the source code just to update data means that your program is at greater risk, since you are editing your source code instead of editing just your data. In addition, your data will not be easy to change and update when stored in the source files, for several obvious reasons. Even with an interpretted scripting language, storing data like students in a classroom, addresses, streets, countries, cities, etc. in the source files is not reasonable or maintainable practise.

About
This site is about programming and other things.
_ _ _